package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.ExamQualItemSet;
import com.sihai.ehr.model.biz.ExamQualStandardSet;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.ExamQualItemSetService;
import com.sihai.ehr.service.ExamQualStandSetService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

/**
 * 定性考核项目设定Action
 * 
 * @author wxn
 * @since 12-10-15 21:20
 * 
 */
@Results({
        @Result(name = "list", location = "exam_qual_item_list.jsp"),
        @Result(name = "add", location = "exam_qual_item_add.jsp"),
        @Result(name = "main", location = "exam_qual_item_main.jsp")
})
public class ExamQualItemAction extends EHRAction {

    private List<Organization> organizations;

    private LimitedList<ExamQualItemSet> examQualItemSets;

    @BeanInject
    private OrganizationService organizationService;

    @BeanInject
    private ExamQualItemSetService examQualItemSetService;

    @BeanInject
    private ExamQualStandSetService examQualStandSetService;

    //考核标准设置实体类列表
    private List<ExamQualStandardSet> examQualStandardSets;

    //考核项目设定
    private ExamQualItemSet examQualItemSet;

    private String ids;

    private float percentage;

    private static DecimalFormat decimalFormat = new DecimalFormat("#.#");

	/**
	 * 
	 */
	@Action("/exam/qual_item_main")
	public String execute() throws Exception {
		listAction = "/exam/qual_item_list.xhtml";
        listTitle = "考核项目设定";
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        return "main";
	}

    @Action("/exam/qual_item_list")
    public String list() throws Exception {

        examQualItemSets = examQualItemSetService.findByTopOrganization(organizationId, createLimit(), createSorter());
        examQualStandardSets = examQualStandSetService.searchExamQualStandardSets(organizationId, createLimit(), createSorter());

        /* 分页 */
        this.paging(examQualItemSets);
        /* 设置翻页action */
        this.setPageUrl("exam/qual_item_list.xhtml");

        return "list";
    }

    //跳转到添加/编辑页面
    @Action("/exam/qual_item_add")
    public String add() throws Exception {
        float currentPercent = 0;
        if(examQualItemSet != null && examQualItemSet.getId() != null && examQualItemSet.getId() > 0){//edit
            examQualItemSet = examQualItemSetService.get(examQualItemSet.getId());
            currentPercent = examQualItemSet.getPercentage();
            listTitle = "修改";
        }else{
        	listTitle = "添加";
        }
        examQualStandardSets = examQualStandSetService.searchExamQualStandardSets(organizationId, createLimit(), createSorter());
        //查询已设置的百分比的和
        Double d = examQualItemSetService.findPercentage(organizationId);
        percentage = 100f + currentPercent - (d == null ? 0f : d.floatValue());
        percentage = Float.parseFloat(decimalFormat.format(percentage));

        return "add";
    }

    //得到定性考核项目未分配的百分比
    @Action(value = "/ajax/exam_qual_item_percent", results = {
        @Result(name = "success", type = "json", params = {"root", "percentage"})
    })
    public String findPercentage(){
        Double d = examQualItemSetService.findPercentage(organizationId);
        percentage = (d == null ? 0f : d.floatValue());
        percentage = Float.parseFloat(decimalFormat.format(percentage));
        return SUCCESS;
    }

    //添加/编辑
    @Action("/exam/qual_item_create")
    public String create() throws Exception {
        if(examQualItemSet != null){
            if(examQualItemSet.getId() != null && examQualItemSet.getId() > 0){//edit
                ExamQualItemSet eqis = examQualItemSetService.get(examQualItemSet.getId());
                eqis.setTitle(examQualItemSet.getTitle());
                eqis.setPercentage(examQualItemSet.getPercentage());
                eqis.setDesc1(examQualItemSet.getDesc1());
                eqis.setDesc2(examQualItemSet.getDesc2());
                eqis.setDesc3(examQualItemSet.getDesc3());
                eqis.setDesc4(examQualItemSet.getDesc4());
                eqis.setDesc5(examQualItemSet.getDesc5());
                eqis.setDesc6(examQualItemSet.getDesc6());
                eqis.setDesc7(examQualItemSet.getDesc7());
                eqis.setDesc8(examQualItemSet.getDesc8());
                eqis.setDesc9(examQualItemSet.getDesc9());
                eqis.setDesc10(examQualItemSet.getDesc10());
                examQualItemSetService.save(eqis);
            }else{//create
                examQualItemSetService.save(examQualItemSet);
            }
        }
        
        return execute();
    }

    @Action("/exam/qual_item_remove")
    public String remove() throws Exception{
        if(StringUtils.isNotBlank(ids)){
            examQualItemSetService.remove(ids);
        }
        return list();
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public LimitedList<ExamQualItemSet> getExamQualItemSets() {
        return examQualItemSets;
    }

    public List<ExamQualStandardSet> getExamQualStandardSets() {
        return examQualStandardSets;
    }

    public ExamQualItemSet getExamQualItemSet() {
        return examQualItemSet;
    }

    public void setExamQualItemSet(ExamQualItemSet examQualItemSet) {
        this.examQualItemSet = examQualItemSet;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public float getPercentage() {
        return percentage;
    }
}
